using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class POrderDAL
    {
        public static int Save(POrderInfo info)
        {
            string sql = string.Format("insert into [porder]([AdminID],[UserID],[QuanID],[Num],[Money],[PostMoney],[ScoreMoney],[QuanMoney],[AllMoney],[Score],[Count],[Logistics],[ExpressNum],[Province],[City],[Area],[RealName],[Mobile],[Address],[State],[CreateDate]) values(@AdminID,@UserID,@QuanID,@Num,@Money,@PostMoney,@ScoreMoney,@QuanMoney,@AllMoney,@Score,@Count,@Logistics,@ExpressNum,@Province,@City,@Area,@RealName,@Mobile,@Address,@State,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@UserID", info.UserID),
                new SqlParameter("@QuanID", info.QuanID),
                new SqlParameter("@Num", info.Num),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@PostMoney", info.PostMoney),
                new SqlParameter("@ScoreMoney", info.ScoreMoney),
                new SqlParameter("@QuanMoney", info.QuanMoney),
                new SqlParameter("@AllMoney", info.AllMoney),
                new SqlParameter("@Score", info.Score),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@Logistics", info.Logistics),
                new SqlParameter("@ExpressNum", info.ExpressNum),
                new SqlParameter("@Province", info.Province),
                new SqlParameter("@City", info.City),
                new SqlParameter("@Area", info.Area),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@Address", info.Address),
                new SqlParameter("@State", info.State),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(POrderInfo info)
        {
            string sql = string.Format("update [porder] set [AdminID]=@AdminID,[Num]=@Num,[Money]=@Money,[PostMoney]=@PostMoney,[ScoreMoney]=@ScoreMoney,[QuanMoney]=@QuanMoney,[AllMoney]=@AllMoney,[Count]=@Count,[Logistics]=@Logistics,[ExpressNum]=@ExpressNum,[Province]=@Province,[City]=@City,[Area]=@Area,[RealName]=@RealName,[Mobile]=@Mobile,[Address]=@Address,[State]=@State,[SendDate]=@SendDate,[ReceiveDate]=@ReceiveDate,[BackDate]=@BackDate where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Num", info.Num),
                new SqlParameter("@Money", info.Money),
                new SqlParameter("@PostMoney", info.PostMoney),
                new SqlParameter("@ScoreMoney", info.ScoreMoney),
                new SqlParameter("@QuanMoney", info.QuanMoney),
                new SqlParameter("@AllMoney", info.AllMoney),
                new SqlParameter("@Count", info.Count),
                new SqlParameter("@Logistics", info.Logistics),
                new SqlParameter("@ExpressNum", info.ExpressNum),
                new SqlParameter("@Province", info.Province),
                new SqlParameter("@City", info.City),
                new SqlParameter("@Area", info.Area),
                new SqlParameter("@RealName", info.RealName),
                new SqlParameter("@Mobile", info.Mobile),
                new SqlParameter("@Address", info.Address),
                new SqlParameter("@State", info.State),
                new SqlParameter("@SendDate", info.SendDate),
                new SqlParameter("@ReceiveDate", info.ReceiveDate),
                new SqlParameter("@BackDate", info.BackDate),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static POrderInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[porder]");
            return SimpleData(dr);
        }
        public static List<POrderInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[porder]");
            return ListSimpleData(dt);
        }
        public static bool UpdateState(int state, int adminid, int userid, int id)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [porder] set State={0} where AdminID={1} and UserID={2} and ID ={3};", state, adminid, userid, id));
        }
        public static bool RemindPOrder(int adminid, int userid, int id)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [porder] set RemindDate='{0}' where AdminID={1} and UserID={2} and ID ={3};", DateTime.Now, adminid, userid, id));
        }
        public static bool SendPOrder(int adminid, int id, string logistics, string expressnum)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [porder] set State=2,Logistics='{0}',ExpressNum='{1}',SendDate='{2}' where AdminID={3} and ID ={4};", logistics, expressnum, DateTime.Now, adminid, id));
        }
        public static List<POrderInfo> FindPage(int adminid, int userid, int state, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (adminid != -1)
            {
                filter.AppendFormat(" and [porder].AdminID ={0}", adminid);
            }
            if (userid != -1)
            {
                filter.AppendFormat(" and [porder].UserID ={0}", userid);
            }
            if (state != -1)
            {
                filter.AppendFormat(" and [porder].State ={0}", state);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [porder].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[porder]", out count);
            return ListSimpleData(dt);
        }
        public static List<POrderInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [porder]"));
            return ListSimpleData(dt);
        }
        public static List<dynamic> FindStateCount(int adminid, int userid)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select state,count(state) as count from porder where AdminID={0} and UserID={1} group by state",adminid , userid));
            List<dynamic> list = new List<dynamic>();
            foreach (DataRow row in dt.Rows)
            {
                dynamic obj = new ExpandoObject();
                obj.State = DataConvert.ToInt(row["state"]);
                obj.Count = DataConvert.ToInt(row["count"]);
                list.Add(obj);
            }
            return list;
        }
        private static POrderInfo SimpleData(DataRow dr)
        {
            POrderInfo info = new POrderInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.QuanID = DataConvert.ToInt(dr["QuanID"]);
                info.Num = DataConvert.ToString(dr["Num"]);
                info.Money = DataConvert.ToDouble(dr["Money"]);
                info.PostMoney = DataConvert.ToDouble(dr["PostMoney"]);
                info.ScoreMoney = DataConvert.ToDouble(dr["ScoreMoney"]);
                info.QuanMoney = DataConvert.ToDouble(dr["QuanMoney"]);
                info.AllMoney = DataConvert.ToDouble(dr["AllMoney"]);
                info.Score = DataConvert.ToInt(dr["Score"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
                info.Logistics = DataConvert.ToString(dr["Logistics"]);
                info.ExpressNum = DataConvert.ToString(dr["ExpressNum"]);
                info.Province = DataConvert.ToString(dr["Province"]);
                info.City = DataConvert.ToString(dr["City"]);
                info.Area = DataConvert.ToString(dr["Area"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.State = DataConvert.ToInt(dr["State"]);
                info.StateText = Commons.OrderState[info.State];
                info.PayDate = DataConvert.ToDateTime(dr["PayDate"]);
                info.SendDate = DataConvert.ToDateTime(dr["SendDate"]);
                info.ReceiveDate = DataConvert.ToDateTime(dr["ReceiveDate"]);
                info.BackDate = DataConvert.ToDateTime(dr["BackDate"]);
                info.RemindDate = DataConvert.ToDateTime(dr["RemindDate"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy-MM-dd HH:mm");
            }
            return info;
        }
        private static POrderInfo AllData(DataRow dr)
        {
            POrderInfo info = new POrderInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.UserID = DataConvert.ToInt(dr["UserID"]);
                info.QuanID = DataConvert.ToInt(dr["QuanID"]);
                info.Num = DataConvert.ToString(dr["Num"]);
                info.Money = DataConvert.ToDouble(dr["Money"]);
                info.PostMoney = DataConvert.ToDouble(dr["PostMoney"]);
                info.ScoreMoney = DataConvert.ToDouble(dr["ScoreMoney"]);
                info.QuanMoney = DataConvert.ToDouble(dr["QuanMoney"]);
                info.AllMoney = DataConvert.ToDouble(dr["AllMoney"]);
                info.Score = DataConvert.ToInt(dr["Score"]);
                info.Count = DataConvert.ToInt(dr["Count"]);
                info.Logistics = DataConvert.ToString(dr["Logistics"]);
                info.ExpressNum = DataConvert.ToString(dr["ExpressNum"]);
                info.Province = DataConvert.ToString(dr["Province"]);
                info.City = DataConvert.ToString(dr["City"]);
                info.Area = DataConvert.ToString(dr["Area"]);
                info.RealName = DataConvert.ToString(dr["RealName"]);
                info.Mobile = DataConvert.ToString(dr["Mobile"]);
                info.Address = DataConvert.ToString(dr["Address"]);
                info.State = DataConvert.ToInt(dr["State"]);
                info.StateText = Commons.OrderState[info.State];
                info.PayDate = DataConvert.ToDateTime(dr["PayDate"]);
                info.SendDate = DataConvert.ToDateTime(dr["SendDate"]);
                info.ReceiveDate = DataConvert.ToDateTime(dr["ReceiveDate"]);
                info.BackDate = DataConvert.ToDateTime(dr["BackDate"]);
                info.RemindDate = DataConvert.ToDateTime(dr["RemindDate"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
                info.CreateText = info.CreateDate.ToString("yyyy-MM-dd HH:mm");

            }
            return info;
        }
        private static List<POrderInfo> ListSimpleData(DataTable dt)
        {
            List<POrderInfo> list = new List<POrderInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<POrderInfo> ListAllData(DataTable dt)
        {
            List<POrderInfo> list = new List<POrderInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

